package com.briup.ch10;

/**
 * @Author briup-adam
 * @Date 2023/10/27 下午3:12
 * @Description
 **/

public class Printer {
    //锁对象 this
    public synchronized void print1() {
        System.out.print("好");
        System.out.print("好");
        System.out.print("学");
        System.out.println("习");
    }

    public void print2() {
        synchronized (this) {
            System.out.print("天");
            System.out.print("天");
            System.out.print("向");
            System.out.println("上");
        }
    }

    //默认锁对象 Printer.class
    synchronized  public static void print3() {
        System.out.print("好");
        System.out.print("好");
        System.out.print("学");
        System.out.println("习");
//        Printer.class.notify();
//        try {
//            Printer.class.wait();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    public static void print4() {
        //Object.class
        synchronized (Printer.class) {
            System.out.print("天");
            System.out.print("天");
            System.out.print("向");
            System.out.println("上");
//            Printer.class.notify();
//            try {
//                Printer.class.wait();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }

    public static void main(String[] args) {
        Printer printer = new Printer();
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    printer.print1();
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    printer.print2();
                }
            }
        };
//        t1.start();
//        t2.start();
        Thread t3 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    Printer.print3();
                }
            }
        };

        Thread t4= new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    Printer.print4();
                }
            }
        };
        t3.start();
        t4.start();
//            t3.notify();


    }
}
